
import { addItemToCart, ReadShoppingCart } from '@a/index'


export default {
  // 命名空间
  namespace: 'count',
  state: {
    items: [],   //购物车的商品信息
    number: 0,  //购买数量
    total: 0   //总价格
  },
  reducers: {
    // 接受异步接口返回数据 赋值给items
    initItems (state: any, action: any) {
      state.items = action.data.items
      state.number = action.data.number
      state.total = action.data.price
      return {
        ...state
      }
    }
  },
  // 处理异步
  effects: {
    //加入购物车的请求
    *addCate(action: any, { put, call }: any) {
      
      const res = yield call(addItemToCart, action.data);
      if (res.data.code === 0) {
        console.log(res);
        
        yield put({ type: 'initItems', data: res.data.data });
      }
      
      // put相当于dispatch
      // 
    },
    //加入购物车的请求

    //读取购物车数据  ReadShoppingCart
    *ReadShoppingCarted(action: any, { put, call }: any) {
      
      const res = yield call(ReadShoppingCart);
      if (res.data.code === 0) {
        console.log(res);
        
        yield put({ type: 'initItems', data: res.data.data });
      }
      
      // put相当于dispatch
      // 
    },
  },
};
        